package com.nbjtjc.safe.util;

import cn.hutool.core.date.DateUtil;
import com.nbjtjc.safe.bean.Constant;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @auther guqianbin
 * @date 2018/7/17 14:02
 **/
public class DateUtils {


    /**
     * 根据 应急响应的时间间隔，算出当前时间所处的发送简报的时间范围
     * @param timeInterval  时间间隔  小时
     * @param publicTime   发布的时间
     * @return
     */
    public static  List<Date> getEmgSendDate(int timeInterval,Date publicTime){
        List<Date> dates=new ArrayList<>();
        Date now = new Date();

        Calendar calendar = DateUtil.calendar(publicTime);
        boolean inTime = true;
        Date startDate = calendar.getTime();
        calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + timeInterval);
        Date endDate = calendar.getTime();
        while (inTime) {
            if (now.after(startDate) && now.before(endDate)) {
                inTime = false;
            } else {
                startDate = endDate;
                calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + timeInterval);
                endDate = calendar.getTime();
            }
        }
        dates.add(startDate);
        dates.add(endDate);
        return dates;
    }

    public static Date plusDate(Date date,int count){
        if(count<=0){
            return date;
        }else{
            Calendar calendar= DateUtil.calendar(date);
            calendar.add(Calendar.DATE,count);
            return calendar.getTime();
        }
    }

    public static List<Date> getPostTimeRange(int type){

        if(Constant.SysPostTimeLimitType.WEEK.equals(type)){
            return getRangeWeek();
        }
        if(Constant.SysPostTimeLimitType.MONTH.equals(type)){
            return getRangeMonth();
        }
        if(Constant.SysPostTimeLimitType.QUARTER.equals(type)){
            return getRangeQuarter();
        }
        if(Constant.SysPostTimeLimitType.HALFYEAR.equals(type)){
            return getRangeHalfYear();
        }
        if(Constant.SysPostTimeLimitType.YEAR.equals(type)){
            return getYearRange();
        }

        return getYearRange();

    }

    /**
     * 获取一年的时间范围
     *
     * @param year
     * @return
     */
    public static List<Date> getYearRange(int year) {
        List<Date> dates = new ArrayList<>();
        String startStr = year + "-01-01 00:00:00";
        String endStr = year + "-12-31 23:59:59";
        Date startDate = DateUtil.parse(startStr, "yyyy-MM-dd hh:mm:ss");
        Date endDate = DateUtil.parse(endStr, "yyyy-MM-dd hh:mm:ss");
        dates.add(startDate);
        dates.add(endDate);
        return dates;
    }
    public static List<Date> getYearRange() {
        List<Date> dates = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH,0);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        //将小时至0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        calendar.set(Calendar.MINUTE, 0);
        //将秒至0
        calendar.set(Calendar.SECOND,0);
        //将毫秒至0
        calendar.set(Calendar.MILLISECOND, 0);
        dates.add(calendar.getTime());
        calendar.set(Calendar.MONTH,11);
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时至23
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        calendar.set(Calendar.MINUTE, 59);
        //将秒至59
        calendar.set(Calendar.SECOND,59);
        //将毫秒至999
        calendar.set(Calendar.MILLISECOND, 999);
        dates.add(calendar.getTime());
        return dates;
    }

    public static List<Date> getDateRange(Date startDate, int dateCount) {
        List<Date> dates = new ArrayList<>();
        dates.add(startDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        for (int i = 1; i < dateCount; i++) {
            calendar.add(Calendar.DATE, 1);
            dates.add(calendar.getTime());
        }

        return dates;
    }

    /**
     * 获取当前周的开始和结束时间
     * @return
     */
    public static List<Date> getRangeWeek() {
        List<Date> dates=new ArrayList<>();
        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK,1);
        //将小时至0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        calendar.set(Calendar.MINUTE, 0);
        //将秒至0
        calendar.set(Calendar.SECOND,0);
        //将毫秒至0
        calendar.set(Calendar.MILLISECOND, 0);
        dates.add(calendar.getTime());
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getActualMaximum(Calendar.DAY_OF_WEEK));
        //将小时至23
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        calendar.set(Calendar.MINUTE, 59);
        //将秒至59
        calendar.set(Calendar.SECOND,59);
        //将毫秒至999
        calendar.set(Calendar.MILLISECOND, 999);
        dates.add(calendar.getTime());
        return dates;
    }

    /**
     * 返回当前月份的开始和结束时间
     * @return
     */
    public static List<Date> getRangeMonth(){
        List<Date> dates=new ArrayList<>();
        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH,1);
        //将小时至0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        calendar.set(Calendar.MINUTE, 0);
        //将秒至0
        calendar.set(Calendar.SECOND,0);
        //将毫秒至0
        calendar.set(Calendar.MILLISECOND, 0);
        dates.add(calendar.getTime());
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时至23
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        calendar.set(Calendar.MINUTE, 59);
        //将秒至59
        calendar.set(Calendar.SECOND,59);
        //将毫秒至999
        calendar.set(Calendar.MILLISECOND, 999);
        dates.add(calendar.getTime());
        return dates;
    }

    /**
     * 返回当前季度的开始和结束时间
     * @return
     */
    public static List<Date> getRangeQuarter(){
        List<Date> dates=new ArrayList<>();
        Calendar calendar=Calendar.getInstance();
        int month=calendar.get(Calendar.MONTH);
        int startMonth=0;
        int endMonth=0;
        if(month<=2){
            startMonth=0;
            endMonth=2;
        }else if (month>2&&month<=5){
            startMonth=3;
            endMonth=5;
        }else if(month>5&&month<=8){
            startMonth=6;
            endMonth=8;
        }else{
            startMonth=9;
            endMonth=11;
        }
        calendar.set(Calendar.MONTH,startMonth);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        //将小时至0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        calendar.set(Calendar.MINUTE, 0);
        //将秒至0
        calendar.set(Calendar.SECOND,0);
        //将毫秒至0
        calendar.set(Calendar.MILLISECOND, 0);
        dates.add(calendar.getTime());
        calendar.set(Calendar.MONTH,endMonth);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时至23
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        calendar.set(Calendar.MINUTE, 59);
        //将秒至59
        calendar.set(Calendar.SECOND,59);
        //将毫秒至999
        calendar.set(Calendar.MILLISECOND, 999);
        dates.add(calendar.getTime());
        return dates;
    }

    public static void main(String[] args) {
        List<Date> dates=new ArrayList<>();
        dates=getRangeWeek();
        System.out.println(dates);
        dates=getRangeMonth();
        System.out.println(dates);
        dates=getRangeQuarter();
        System.out.println(dates);
        dates=getRangeHalfYear();
        System.out.println(dates);
    }
    /**
     * 获取当前所在半年的开始结束时间
     * @return
     */
    public static List<Date> getRangeHalfYear(){
        List<Date> dates=new ArrayList<>();
        Calendar calendar=Calendar.getInstance();
        int month=calendar.get(Calendar.MONTH);
        int startMonth=0;
        int endMonth=0;
        if(month<=5){
            startMonth=0;
            endMonth=5;
        }else  {
            startMonth = 6;
            endMonth = 11;
        }
        calendar.set(Calendar.MONTH,startMonth);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        //将小时至0
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        calendar.set(Calendar.MINUTE, 0);
        //将秒至0
        calendar.set(Calendar.SECOND,0);
        //将毫秒至0
        calendar.set(Calendar.MILLISECOND, 0);
        dates.add(calendar.getTime());
        calendar.set(Calendar.MONTH,endMonth);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时至23
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        calendar.set(Calendar.MINUTE, 59);
        //将秒至59
        calendar.set(Calendar.SECOND,59);
        //将毫秒至999
        calendar.set(Calendar.MILLISECOND, 999);
        dates.add(calendar.getTime());
        return dates;
    }
}
